package com.atguigu.test.demo;

/**
 * 官网线程类 完成卖水果的动作
 */
public class NetShop implements Runnable {
    private Fruit fruit;

    public NetShop(Fruit fruit) {
        this.fruit = fruit;
    }

    @Override
    public void run() {
        synchronized (Demo.class) {
            while (fruit.getStock() > 0) {

                if (fruit.getStock() % 2 == 0) {
                    Integer stock = fruit.getStock();
                    System.out.println("网店正在卖出" + stock-- + "份,还剩下" + stock + "份");
                    fruit.setStock(stock);
                } else {
                    try {
                        Demo.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Demo.class.notifyAll();
            }

        }
    }
}
